package mafiagame;

import mafiagame.Mark.MarkType;

public class WillAction extends Action
{
	Game myGame;
	Player myPlayer;

	public WillAction(Game g, Player p)
	{
		myGame = g;
		myPlayer = p;
	}

	@Override
	public String getDescription()
	{
		return "You have been killed! Please leave your will to another player. Wills should be of"
				+ " the format 'will_to [player number] [your will text here]', without the brackets.";

	}

	@Override
	public String getCommand()
	{
		return "will_to";
	}

	@Override
	public boolean Execute(String message)
	{
		String[] tokens = message.split(" ");
		String temp = "";
		String[] tempTokens = new String[3];

		if (tokens.length > 3)
		{
			for (int n = 2; n < tokens.length; n++)
				temp = temp + " " + tokens[n];
			for (int n = 0; n < 2; n++)
				tempTokens[n] = tokens[n];
			tempTokens[2] = temp.substring(1);
			tokens = tempTokens;
		}

		if (tokens.length != 3)
		{
			myGame.communicator
					.sendPrivateMessage(
							myPlayer.username,
							"Your command must be "
									+ "of the form 'will_to [player number] [your will text here]', without the brackets.");
			return false;
		}
		Player recipient = myGame.parsePlayer(tokens[1]);
		if (recipient == null)
		{
			myGame.communicator.sendPrivateMessage(myPlayer.username,
					"You must choose a player who is actually playing.");
			return false;
		}
		myGame.communicator.sendPrivateMessage(myPlayer.username,
				"You have sent a will to " + recipient.username);
		if (recipient.living)
		{
			Mark willMark = new Mark();
			willMark.type = MarkType.GIVE_WILL;
			willMark.source = myPlayer;
			willMark.willContents = tokens[2];
			recipient.marks.add(willMark);
		}
		myPlayer.gaveWill = true;
		myGame.numGaveWill++;
		myGame.tryEndWill();

		return true;
	}

	@Override
	public boolean isPublic()
	{
		return false;
	}

}
